package server.auth;


import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *	Parser du fichier xml contenant les informations des clients 
 *
 */
public class Server_Auth {

	/**
	 * Récupération d'un client et l'authentifie ou non
	 * @param pseudo
	 * @param password
	 * @return
	 */
	public static boolean get_authentification(String pseudo, String password){

		try {
			File file = new File("Authentification/list_clients.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			NodeList nodeLst = doc.getElementsByTagName("client");
			for (int s = 0; s < nodeLst.getLength(); s++) {
				Node fstNode = nodeLst.item(s);
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					Element fstElmnt = (Element) fstNode;
					if(fstElmnt.getAttribute("pseudo").equals(pseudo)){
						NodeList pass = fstElmnt.getElementsByTagName("password");
						if(pass.getLength() >=1 && pass.item(0).getTextContent().equals(password))
							return true;
					}
				}

			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

}
